
myApp.controller('userCtrl', ['$scope', '$location',  '$http',function ($scope, $location, $http) {
    _log('### userCtrl ###');

    $scope.users = {};


//    $.getJSON(_COMMON.context + url, params).done(function (result) {
//        $scope.$apply(function () {
//            $scope.users = result.users;
//        });
//        $scope.$apply(function () {
//            $("table").tablesorter();
//        });
//    });
    
    
    var renderTable = function(){
	
	var url = 'user_ws.php';
	var params = {
	        wsname: 'getallusers',
	        token: _pageData.tokenKey
	};
	var rootData = 'users';
	
	 _log('### renderTable ###');
	 
	var t = $('table').dataTable( {
            "ajax":{ 
                    url: _COMMON.context + url+"?wsname="+params.wsname+"&token="+params.token,
                    dataSrc: rootData
                },
            "columns": [
                { "data": "id",  "sClass" : "center"},
                { "data": "username" },
                { "data": "displayname" },
                { "data": "email" },
                { "data": "firstname" },
                { "data": "lastname" },
                { "data": "birthday" , "sClass" : "center"},
                { "data": "age",  "sClass" : "center"},
                { "data": "usertype"},
                { "data": "status",  "sClass" : "center"}
            ],
            "columnDefs": [ 
                   {
                       "render": function ( data, type, row ) {
                	   return _COMMON.enum.userType[data];
                       },
                       "targets": 8
                   },
                   {
                       "render": function ( data, type, row ) {
                	   return '<font style="color: '+_COMMON.enum.status[data].color+'">'
                	   +_COMMON.enum.status[data].value+'</font>';
                       },
                       "targets": 9
                   },
                   {
                       "render": function ( data, type, row ) {
                	   return '<button title="Edit" type="button" class="btn btn-warning btn-table-action" onclick="_pageFn.onUserEdit('+data+')"><i class="fa fa-nontext fa-edit"></i></button>&nbsp;'+
                   	'<button title="Delete"type="button" class="btn btn-danger btn-table-action" onclick="_pageFn.onUserDelete('+data+')"><i class="fa fa-nontext fa-trash-o"></i></button>';
                       },
                       "targets": 0
                   }
               ],
//		"sScrollY": "300px",
		"sScrollX": "100px",
		"sScrollXInner": "100%",
		"bScrollCollapse": true,
		"bPaginate": true,
		"sPaginationType": "full_numbers"
                      
        } );
	
	new $.fn.dataTable.FixedColumns( t, {
        	"iLeftColumns": 1,
        	"iRightColumns": 0
        } );
	
    };

    _pageFn.onUserEdit = function (id) {
        location = '#/admin/user/user?id='+id;
    };
    
    

    _pageFn.onUserDelete = function (id) {

        $("#confirm").html("Do you want to delete ?");

        $("#confirm").dialog({
        resizable: false,
        modal: true,
        title: "Confirm Delete",
        height: 180,
        width: 300,
        buttons: {
            "Yes": function () {
                $(this).dialog('close');
                callback(id);
            },
                "No": function () {
                $(this).dialog('close');
            }
        }
    });
    };
       
    
    function callback(value) {
        if (value) {
      
            //alert("Confirmed");
             var config = {
	            params : {
		        wsname : 'deleteuser',
		        token : _pageData.tokenKey,
		        id : value
	            }
	        };
	
	        $http.post(_COMMON.context + url, null, config)
	          .success(function(obj) {
	              //alert('Delete Success !');
                    notif({
                      msg: "<b><i class='fa fa-thumbs-o-up'></i>  Delete Success !</b>",
                      type: "success",
                      position: "center"
                    });

                    location= '#/admin/user/list/'; //refresh.

                  })
                  .error(function() {
                      alert('Error is occured, May be database connect is wrong');
                  });
       

        } else {
            alert("Rejected");
        }

    };
    
    // Start Render table 
    renderTable();

}]);